<div id="new-event-dlg" style="font-size: 62.5%; display: none;" title="Nowa rezerwacja">
	<form id="new-event-form" method="post" action="<?php echo Yii::app()->request->baseUrl; ?>/index.php/calendar/addReservation">
		<p class="validateTips"></p>
		<span style='display: block; margin-top: 10px; color: blue;'>
			<b><label for="station">Stanowisko: </label></b>
		</span>
		<select id="new-event-station" name="idKeeperReserv" class="stations" >
		</select>
		<label for="name">Nazwa: </label>
		<input id="name" class="name" name="name">
		<span style='display: block; margin-top: 10px; color: blue;'>
			<b>Termin rezerwacji:</b> 
		</span> 
		<label>Czas: </label> <input id="starttime"	class="starttime" name="starttime" size="4" style='display: inline;'>
		<label for="station"> - </label> <input id="endtime" class="endtime" name="endtime" size="4" style='display: inline;'> <label
			style='margin-left: 20px;'>Data: </label> <input id="reservationdate"
			class="reservationdate" name="reservationdate" size="9"
			style='display: inline;'
		>
		<span style='display: block; margin-top: 10px; color: blue;'>
			<b><label for="limit">Limit studentów: </label></b>
		</span>
		<input id="limit" class="limit" name="limit">
	</form>
</div>

<script>

function updateTips( t ) {
	var tips = $("#new-event-dlg .validateTips");
	tips.text( t ).addClass( "ui-state-highlight" );
	setTimeout(function() {
		tips.removeClass( "ui-state-highlight", 1500 );
	}, 500 );
};

function is_int(value){
	  if((parseFloat(value) == parseInt(value)) && !isNaN(value)){
	      return true;
	  } else {
	      return false;
	  }
	}

function setEventTimesToChoosenStation() {
	var selectedStation = $('#new-event-form .stations option:selected');
	if ( selectedStation.length > 0 ) {
		var start = new Date(selectedStation.attr('startval')*1000);
		var end =  new Date(selectedStation.attr('endval')*1000);
		$('#new-event-form .starttime')
			.datepicker('setDate', start)
			.datepicker("option", 'minDate', start)
			.datepicker("option", 'maxDate', end);
		$('#new-event-form .endtime')
		.datepicker('setDate', new Date(selectedStation.attr('endval')*1000))
		.datepicker("option", 'minDate', start)
		.datepicker("option", 'maxDate', end);
		$('#new-event-form .reservationdate').datepicker('setDate', start);

	}
}

function unixDateToString(date) {
	return getJSDateTime(new Date(date*1000));
}
function readStations(data) {
	var stations = data.stations;
	$("#new-event-station").html('');
	for ( var i = 0; i < stations.length; i++ ) {
		var station = stations[i];
		$("#new-event-station").append(
			'<option value="'
				+station.id+'" start="'
				+unixDateToString(station.start)+'" end="'
				+unixDateToString(station.end)+'" startval="'+station.start+'" endval="'+station.end+'">'
					+station.name+'</option>'
		);
	}

	setEventTimesToChoosenStation();
}
function validateNewEvent() {
	if ( !is_int($("#new-event-dlg .limit").val()) ) {
		updateTips('Limit musi być liczbą całkowitą');
		return false;
	}
	if ( $("#new-event-dlg .name").val().length < 1 ) {
		updateTips('Nazwa jest wymagana');
		return false;
	}
	return true;
}

$(function() {
	$( "#new-event-dlg" ).dialog({
		autoOpen: false,
		height: 490,
		width: 390,
		modal: true,
		resizable: false,
		buttons : {
			"Ok" : function() {
				if ( validateNewEvent() ) {
					$.post(
						'<?php echo Yii::app()->request->baseUrl; ?>/index.php/calendar/addTeacherReservation',
						$("#new-event-form").serialize(),
						function(data) {
							$('#calendar').fullCalendar( 'refetchEvents' );
						},
						"json"
					);
					$( this ).dialog( "close" );
				}
			},	
			"Anuluj": function() {
				$( this ).dialog( "close" );
			}
		},
		open: function() {
			$("#new-event-dlg .validateTips").html("");
		}
	});

	var reservationDate=$('#new-event-form .reservationdate').datepicker({ 
		dateFormat: 'dd/mm/yy',
		//minDate:new Date(),
		onClose: function (ev){
	    	   $.post(
						'<?php echo Yii::app()->request->baseUrl;?>/index.php/calendar/availableStationsForTeacher',
						{
							day : getDayNew(),
							month : getMonthNew(),
							year : getYearNew(),
							hourStart : getHourStartNew(),
							hourStop : getHourStopNew(),
							minStart : getMinuteStartNew(),
							minStop : getMinuteStopNew()
						},
						readStations,
						"json"
					);
	       },
	});

	var startTime=$('#new-event-form .starttime').timepicker({ 
		timeFormat: 'HH:mm',
		onSelect: function (dateText, inst) {
			var temp = $('#new-event-form .starttime').datepicker('getDate');
			var selectedStation = $('#new-event-form .stations option:selected');
			if ( selectedStation.length > 0 ) {
				var startDate = new Date(selectedStation.attr('start'));
				var endDate = new Date(selectedStation.attr('end'));
				if ( startDate.getHours()*60 + startDate.getMinutes() > temp.getHours()*60 + temp.getMinutes() ) {
					temp.setHours(startDate.getHours());
					temp.setMinutes(startDate.getMinutes());
				}
				if ( endDate.getHours()*60 + endDate.getMinutes() < temp.getHours()*60 + temp.getMinutes() ) {
					temp.setHours(endDate.getHours());
					temp.setMinutes(endDate.getMinutes());
				}
			}
			
       		if(temp.getHours() < 6){
				temp.setHours(6);
    		}
       		if((temp.getHours() > 21 && temp.getMinutes() > 30) || temp.getHours() > 22){
				temp.setHours(21);
				temp.setMinutes(30);
    		}
       		$('#new-event-form .starttime').datepicker('setDate', temp);
       		temp.setDate(temp.getDate());
            temp.setMinutes(temp.getMinutes()+30);
            
       },
       onClose: function (ev){ },
       hourMin: 6,
       hourMax: 21,
       hourGrid: 2,
       minuteGrid: 10
	});

	var endTime=$('#new-event-form .endtime').timepicker({ 
		timeFormat: 'HH:mm',
		onSelect: function (dateText, inst) {
            var temp = $('#new-event-form .endtime').datepicker('getDate');

            var selectedStation = $('#new-event-form .stations option:selected');
			if ( selectedStation.length > 0 ) {
				var startDate = new Date(selectedStation.attr('start'));
				var endDate = new Date(selectedStation.attr('end'));
				if ( startDate.getHours()*60 + startDate.getMinutes() > temp.getHours()*60 + temp.getMinutes() ) {
					temp.setHours(startDate.getHours());
					temp.setMinutes(startDate.getMinutes());
				}
				if ( endDate.getHours()*60 + endDate.getMinutes() < temp.getHours()*60 + temp.getMinutes() ) {
					temp.setHours(endDate.getHours());
					temp.setMinutes(endDate.getMinutes());
				}
			}
            
       		if(temp.getHours() > 22){
       			temp.setHours(22);
    		}
       		var temp2 = $('#new-event-form .starttime').datepicker('getDate');
       		if( temp.getHours()*60 + temp.getMinutes() < temp2.getHours()*60 + temp2.getMinutes()){
				temp.setHours(temp2.getHours());
				temp.setMinutes(temp2.getMinutes());
           	}
       		$('#new-event-form .endtime').datepicker('setDate', temp);
       },
       hourMin: 6,
       hourMax: 21,
       hourGrid: 2,
       minuteGrid: 10
       });


	 $('#new-event-form .stations').change(setEventTimesToChoosenStation);
});

function getDayNew()
{
	return $('#new-event-form .reservationdate').datepicker( "getDate" ).getDate();
}

function getMonthNew()
{
	return $('#new-event-form .reservationdate').datepicker( "getDate" ).getMonth()+1;
}

function getYearNew()
{
	return $('#new-event-form .reservationdate').datepicker( "getDate" ).getFullYear();
}

function getHourStartNew()
{
	return $('#new-event-form .starttime').datepicker( "getDate" ).getHours();
}

function getMinuteStartNew()
{
	return $('#new-event-form .starttime').datepicker( "getDate" ).getMinutes();
}

function getHourStopNew()
{
	return $('#new-event-form .endtime').datepicker( "getDate" ).getHours();
}

function getMinuteStopNew()
{
	return $('#new-event-form .endtime').datepicker( "getDate" ).getMinutes();
}
</script>